package com.forever7776.life.web.controller.life.m;

import com.alibaba.fastjson.JSONObject;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.common.vo.SysUserCpwdVO;
import com.forever7776.life.core.entity.sys.SysUser;
import com.forever7776.life.core.service.sys.ISysUserService;
import com.forever7776.life.remote.common.enums.ResultEnum;
import com.forever7776.life.remote.common.vo.ResultVO;
import com.forever7776.life.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import util.ResultUtil;

/**
 * 用户首页
 *
 * @author kz
 * @date 2017年12月29日14:32:07
 */
@Controller
@RequestMapping(value = "/m")
public class MyController extends BaseController {

    @Autowired
    private ISysUserService iSysUserService;

    @RequestMapping
    public String index() {
        return USER_VIEW_PAGE_PATH + "index";
    }

    /**
     * 个人资料
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info(ModelMap map) {
        map.put("m", iSysUserService.selectById(getUserId()));
        return USER_VIEW_PAGE_PATH + "info";
    }

    /**
     * 个人资料保存
     *
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject info(SysUser sysUser) {
        String userId = sysUser.getId();
        if (userId == null || !userId.equals(getUserId())) {
            return ResultUtil.getFailResult("更新失败~~");
        }
        iSysUserService.updateById(sysUser);
        return ResultUtil.getSuccessResult("更新信息成功~");
    }

    /**
     * 修改密码页面
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/cpwd", method = RequestMethod.GET)
    public String cpwd(ModelMap map) {
        map.put("m", iSysUserService.selectById(getUserId()));
        return USER_VIEW_PAGE_PATH + "cpwd";
    }

    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping(value = "/cpwd", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO cpwd(SysUserCpwdVO sysUserCpwdVO) {
        String userId = request.getParameter("id");
        if (userId == null || !userId.equals(getUserId())) {
            return ResultVoUtil.getFailResult("修改密码失败~~");
        }
        ResultVO res = checkCpwd(userId, sysUserCpwdVO);
        if (res.getCode().equals(ResultEnum.STATUS.FAIL.getCode())) {
            return res;
        }
        iSysUserService.cpwd(userId, sysUserCpwdVO.getNewPwd());
        SecurityUtils.getSubject().logout();
        return ResultVoUtil.getSuccessResult("修改密码成功~");
    }

    public ResultVO checkCpwd(String userId, SysUserCpwdVO vo) {
        if (StringUtils.isEmpty(vo.getOldPwd())) {
            return ResultVoUtil.getFailResult("请输入旧密码~");
        }
        if (StringUtils.isEmpty(vo.getNewPwd())) {
            return ResultVoUtil.getFailResult("请输入新密码~");
        }
        if (!vo.getAgainPwd().equals(vo.getNewPwd())) {
            return ResultVoUtil.getFailResult("两次密码不匹配~");
        }
        if (!iSysUserService.selectById(userId).getPassword().equals(vo.getOldPwd())) {
            return ResultVoUtil.getFailResult("请输入正确的旧密码~");
        }
        return ResultVoUtil.getSuccessResult("");
    }
}
